package com.ludaxu.google.fragment;

import java.util.List;

import com.lidroid.xutils.BitmapUtils;
import com.ludaxu.google.R;
import com.ludaxu.google.domain.AppInfo;
import com.ludaxu.google.tools.BitmapHelper;
import com.ludaxu.google.tools.ViewUtils;
import com.ludaxu.google.view.LoadingPage;
import com.ludaxu.google.view.LoadingPage.loadResult;

import android.os.Bundle;
import android.os.SystemClock;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.FrameLayout;

public abstract class BaseFragment extends Fragment {
	private LoadingPage loadingPage;
	protected BitmapUtils bitmapUtils;

	@Override
	@Nullable
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		bitmapUtils = BitmapHelper.getBitmapUtils();
		if (loadingPage == null) {
			loadingPage = new LoadingPage(getActivity()) {

				@Override
				public loadResult load() {
					return BaseFragment.this.load();
				}

				@Override
				public View createSuccessView() {
					return BaseFragment.this.createSuccessView();
				}
			};
		} else {
			// 先干掉之前的爹
			ViewUtils.removeParent(loadingPage);// 移除frameLayout之前的爹
		}

		return loadingPage;
	}

	/**
	 * 创建成功界面
	 * 
	 * @return
	 */
	public abstract View createSuccessView();

	/**
	 * 请求服务器
	 * 
	 * @return
	 */
	public abstract loadResult load();

	public void show() {
		if (loadingPage != null) {
			loadingPage.show();
		}
	}

	/**
	 * 校验数据
	 * 
	 * @param datas
	 * @return
	 */
	protected loadResult checkData(List datas) {
		if (datas == null) {// 请求服务器失败
			return loadResult.error;
		} else {
			if (datas.size() == 0) {
				return loadResult.empty;// 服务器没有数据
			} else {
				return loadResult.success;// 请求服务器成功
			}
		}
	}

}
